﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net.Mail;
using System.Net;

namespace SP.OpenSource.CDC.Monitor.BLL
{
    public class EmailHelper
    {
        public bool SendMailMessage(String sEmailServer,bool bAuthRequired,String sAccount ,String sPass ,
                                            bool bUseDefaultPort, bool bUseSSL,String sPort,
                                           string from, string to, string subject, string body, 
                                           ref String sErrorMessage)
        {
            try
            {
                
                MailMessage mMailMessage = new MailMessage();
                string[] sToEmails = to.Split(',');

                foreach (string sToEmail in sToEmails)
                {
                    mMailMessage.To.Add(new MailAddress(sToEmail));
                }

                mMailMessage.Subject = subject;
                mMailMessage.Body = body;

                mMailMessage.IsBodyHtml = true;
                mMailMessage.Priority = MailPriority.High;

                SmtpClient mSmtpClient = null;
                if(true == bUseDefaultPort )
                    mSmtpClient = new SmtpClient(sEmailServer);
                else
                    mSmtpClient = new SmtpClient(sEmailServer, Convert.ToInt32(sPort));

                if (from.Length <= 0 && sAccount.Length > 0)
                    mMailMessage.From = new MailAddress(sAccount);
                else
                    mMailMessage.From = new MailAddress(from);

                mSmtpClient.Host = sEmailServer;

                if (true == bAuthRequired)
                {
                    mSmtpClient.Credentials = new NetworkCredential(sAccount,
                                sPass, "");
                }
                else
                    mSmtpClient.UseDefaultCredentials = true;
                
                //Use SSL
                if (true == bUseSSL)
                {
                    mSmtpClient.EnableSsl = true;
                    mSmtpClient.Port = Convert.ToInt32(sPort);
                }

                mSmtpClient.Send(mMailMessage);
               
                sErrorMessage = String.Empty;
                return true;
            }
            catch (Exception ex)
            {
                sErrorMessage = ex.Message;
                return false;
            }
        }


        public String PrepareMsg(String sFilePath,String sTemplateName, String sServerName, String sCDCStatus, ref DataTable dtErrorLog,
                                bool bIsTestMessage = false)
        {
            String sMsg = String.Empty;
            try
            {   
                sMsg = File.ReadAllText(sFilePath+sTemplateName);

                String sErrorLogTable = String.Empty;
                
                sMsg = sMsg.Replace("|SERVER_NAME|", sServerName);

                if (bIsTestMessage == true)
                {
                    //sMsg = sMsg.Replace("|CDC_STATUS|", "<span class=\"StatusGreen\"><CDC Status></span>");
                    if (sCDCStatus == "Enabled")
                        sMsg = sMsg.Replace("|CDC_STATUS|", "<span class=\"StatusGreen\">Enabled</span>");
                    else
                        sMsg = sMsg.Replace("|CDC_STATUS|", "<span class=\"StatusRed\">Disabled</span>");

                    sMsg = sMsg.Replace("|ERROR_LOG|", "This is test message. Actual alert message will contain CDC status & error log information here.");
                }
                else
                {
                    if (sCDCStatus == "Enabled")
                        sMsg = sMsg.Replace("|CDC_STATUS|", "<span class=\"StatusGreen\">Enabled</span>");
                    else
                        sMsg = sMsg.Replace("|CDC_STATUS|", "<span class=\"StatusRed\">Disabled</span>");

                    if (null != dtErrorLog && dtErrorLog.Rows.Count > 0)
                        sErrorLogTable = PrepareErrorLogTable(ref dtErrorLog);

                    sMsg = sMsg.Replace("|ERROR_LOG|", sErrorLogTable);
                }
                
                return sMsg;
            }
            catch (Exception ex)
            {
                return String.Empty;
            }
        }

        private String PrepareErrorLogTable(ref DataTable dtIN)
        {
            try
            {
                String sErrorLogHTMLTable = String.Empty;
                sErrorLogHTMLTable = " <table cellspacing=\"0\" cellpadding=\"4\" rules=\"rows\" id=\"CDCErrorLog\" ";
                sErrorLogHTMLTable += "style=\"border: 1px None #CCCCCC; color:Black; background-color:White;width:1097px; border-collapse:collapse; \">";
                //Heading row
                sErrorLogHTMLTable += PrepareErrorLogHeader();
                sErrorLogHTMLTable += PrepareErrorLogColumns(ref dtIN);
                sErrorLogHTMLTable += PrepareErrorLogRows(ref dtIN);

                sErrorLogHTMLTable += "</table>";
                return sErrorLogHTMLTable;
            }
            catch (Exception ex)
            {
                return String.Empty;
            }
        }
        private String PrepareErrorLogRows(ref DataTable dtIN)
        {
            try
            {
                String sErrorLogHTMLTable = "<tr style=\"border-right-style: solid; border-right-width: thin; border-right-color: #C0C0C0\" >";
                
                foreach(DataRow drT in dtIN.Rows)
                {
                    foreach (DataColumn dcT in dtIN.Columns)
                    {
                        sErrorLogHTMLTable += "<td>";
                        sErrorLogHTMLTable += drT[dcT.ColumnName].ToString();
                        sErrorLogHTMLTable += "</td>";
                    }
                        
                }
                sErrorLogHTMLTable += "</tr>";

                return sErrorLogHTMLTable;
            }
            catch (Exception ex)
            {
                return String.Empty;
            }
        }

        private String PrepareErrorLogHeader()
        {
            String sErrorLogHeader = String.Empty;

             sErrorLogHeader += "<tr style=\"color:White;background-color:#333333;font-weight:bold;\">";
			sErrorLogHeader += "<th align=\"left\" scope=\"col\" colspan=\"10\" ";
            sErrorLogHeader += "style=\"font-family: 'Arial Black'; font-size: medium\">CDC Error Log</th>";
			sErrorLogHeader += "<th align=\"left\" scope=\"col\" ";
            sErrorLogHeader += "style=\"font-family: 'Arial Black'; font-size: small\">&nbsp;</th>";
            sErrorLogHeader += "</tr>";

            return sErrorLogHeader;
        }

        private String PrepareErrorLogColumns(ref DataTable inDT)
        {
            String sErrorLogColumns = String.Empty;

            sErrorLogColumns = "<tr style=\"color:White;background-color:#333333;font-weight:bold;\">";
            
            foreach(DataColumn dcT in inDT.Columns)
            {
                sErrorLogColumns += "<th align=\"left\" scope=\"col\" style=\"font-family: 'Arial Black';  border-right-style: solid; border-right-width: thin; border-right-color: #FFFFFF; class=\"style5\" \" >";
                sErrorLogColumns += dcT.ColumnName;
                sErrorLogColumns += "</th>";
            }

            sErrorLogColumns += "</tr>";
            return sErrorLogColumns;
        }
   }
}
